#include "../include/TaskQue.hpp"

namespace cdm{

TaskQue::TaskQue(size_t size)
:_que(),
_queSize(size),
_mutex(),
_notFull(_mutex),
_notEmpty(_mutex),
_stop(false){
    
}

TaskQue::~TaskQue(){

}

bool TaskQue::isFull(){
    return _que.size() == _queSize;
}

bool TaskQue::isEmpty(){
    return _que.size() == 0;
}

void TaskQue::push(EleType e){
    MutexLockGuard autolock(_mutex);
    while(isFull()){
        _notFull.wait();
    }
    _que.push(e);
    _notEmpty.notifyOne();
}

EleType TaskQue::pop(){
    MutexLockGuard autolock(_mutex);
    EleType tmp;
    while(isEmpty()&& !_stop){
        _notEmpty.wait();
    }
    if(!_stop){
        tmp = _que.front();
        _que.pop();
        _notFull.notifyOne();
    }else{
        tmp = nullptr;
    }
    return tmp;
}

void TaskQue::wakeUp(){
    _stop = true;
    _notEmpty.notifyAll();

}

}//end of namespace


